﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LeHouse.Server.Dto;
namespace LeHouse.Server.Dal
{
    using ServiceStack.OrmLite;
    using System.Linq.Expressions;
    using System.Data;
    /// <summary>
    /// 员工信息的DAL层
    /// </summary>
    public class System_StaffDAL : GenericBaseDAL<System_Staff>
    {
        /// <summary>
        /// 用户的数据添加And编辑
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        public bool StaffInsertOrUpdate(System_Staff parms)
        {
            using (var cnn = DbConnection)
            {
                //开启事物
                var cbt = cnn.OpenTransaction();
                try
                {
                    if (parms.T_StaffID <= 0)
                    {
                        long key = cnn.Insert(parms, true);

                        for (int i = 0; i < parms.T_Department.Length; i++)
                        {
                            cnn.Insert(new System_DeptStaff()
                            {
                                T_StaffID = (int)key,
                                T_DepartmentID = parms.T_Department[i]
                            });
                        }
                        for (int i = 0; i < parms.T_Role.Length; i++)
                        {
                            cnn.Insert(new System_RoleStaff()
                            {
                                T_StaffID = (int)key,
                                T_RoleID = parms.T_Role[i]
                            });
                        }
                    }
                    else
                    {
                        cnn.Update(parms, t => t.T_StaffID == parms.T_StaffID);

                        cnn.Delete<System_DeptStaff>(t => t.T_StaffID == parms.T_StaffID);
                        cnn.Delete<System_RoleStaff>(t => t.T_StaffID == parms.T_StaffID);

                        for (int i = 0; i < parms.T_Department.Length; i++)
                        {
                            cnn.Insert(new System_DeptStaff()
                            {
                                T_StaffID = parms.T_StaffID,
                                T_DepartmentID = parms.T_Department[i]
                            });
                        }
                        for (int i = 0; i < parms.T_Role.Length; i++)
                        {
                            cnn.Insert(new System_RoleStaff()
                            {
                                T_StaffID = parms.T_StaffID,
                                T_RoleID = parms.T_Role[i]
                            });
                        }
                    }
                    //提交
                    cbt.Commit();
                    return true;
                }
                catch
                {
                    //回滚
                    cbt.Rollback();
                    return false;
                }
            }
        }

        /// <summary>
        /// 删除员工
        /// </summary>
        /// <param name="T_StaffID"></param>
        /// <returns></returns>
        public bool StaffDel(int T_StaffID)
        {
            using (var cnn = DbConnection)
            {
                //开启事物
                var cbt = cnn.OpenTransaction();
                try
                {
                    cnn.Delete<System_Staff>(t => t.T_StaffID == T_StaffID);
                    cnn.Delete<System_DeptStaff>(t => t.T_StaffID == T_StaffID);
                    cnn.Delete<System_RoleStaff>(t => t.T_StaffID == T_StaffID);
                    //提交
                    cbt.Commit();
                    return true;
                }
                catch
                {
                    //回滚
                    cbt.Rollback();
                    return false;
                }
            }
        }

    }
    /// <summary>
    /// 员工菜单信息的DAL层
    /// </summary>
    public class System_UserMenuDAL : GenericBaseDAL<System_UserMenu>
    {
    }
}
